package utilities;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


/**
 * Parser de archivo .json
 * @author ELTIO
 */
public class Mapping {
	
	private static JSONParser parser = new JSONParser();
	
	public static void parseJSON(HashMap<String, String> mapping) throws org.json.simple.parser.ParseException {
		try {
			Object obj = parser.parse(new FileReader(System.getProperty("user.dir") + MediatorConfiguration.getInstance().getProperty(MediatorConfiguration.JSON_MAPPING_PATH)));
	 
			JSONObject jsonObject = (JSONObject) obj;
	 
			JSONArray actions = (JSONArray) jsonObject.get(MediatorConfiguration.getInstance().getProperty(MediatorConfiguration.JSON_ACTIONS_KEY));
			Iterator<JSONObject> iterator = actions.iterator();
			
			JSONObject line = iterator.next();
			String agentValue;
			String toolValue;
			
			while (iterator.hasNext()) {
				 				
				agentValue = (String) line.get(MediatorConfiguration.getInstance().getProperty(MediatorConfiguration.JSON_AGENT_VALUE_KEY));
				toolValue = (String) line.get(MediatorConfiguration.getInstance().getProperty(MediatorConfiguration.JSON_TOOL_VALUE_KEY));
				line = iterator.next();
				mapping.put(toolValue, agentValue);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
     }

}